<template>
	<view>
		<!-- 轮播图 -->
		<u-swiper :list="slides" name="img_url" height="320"></u-swiper>
		
		<!-- 标签页 -->
		<view class="u-text-center u-m-t-30">
			<u-tabs :list="srotList" bar-width="80" :is-scroll="false" :current="current" @change="srotChange"></u-tabs>
		</view>
		
		<!-- 商品列表 -->
		<u-row gutter="16" class="u-skeleton">
			<u-col span="6" v-for="(item,index) in goods" :key='index'>
					
				<goods-card :item="item"></goods-card>
				
			</u-col>
			
		</u-row>
		
		<!--骨架屏-->
		<u-skeleton :loading="loading" :animation="true" bgColor="#FFF"></u-skeleton>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
				srotList:[
					{name: '默认'},
					{name: '销量'},
					{name: '推荐'},
					{name: '最新'}
					],
				current: 0,
				slides:[],
				goods:[],
				page:1,
				loading:false,
				goods:[{},{},{},{}],
			}
		},
		onLoad() {
			// 调用网络请求首页数据函数
			this.getData()
		},
		// 下拉获取更多
		onReachBottom() {
			// 重新请求数据,带上分页参数
			this.page = this.page + 1
			this.getData()
			
		},
		methods: {
			srotChange(index) {
				console.log(index);
				this.current = index;
				// 先清空商品数据和页数
				this.goods = [{},{},{},{}]
				this.page = 1
				this.getData()
			},
			async getData(){
				this.loading = true //显示骨架屏
				const params = {
					page:this.page
				}
				//增加排序条件
				if(this.current == 1) params.sales =1
				if(this.current == 2) params.recommend =1
				if(this.current == 3) params.new =1
				const res = await this.$u.api.index(params)
				this.loading = false //隐藏骨架屏
				this.slides = res.slides
				// this.goods = [...this.goods,...res.goods.data]
				this.goods = this.goods.pop().title ? [...this.goods,...res.goods.data] : res.goods.data
			}
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
